/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.app.modular.vip.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.app.modular.vip.entity.AppVipOrder;
import vip.xiaonuo.app.modular.vip.enums.AppVipOrderExceptionEnum;
import vip.xiaonuo.app.modular.vip.mapper.AppVipOrderMapper;
import vip.xiaonuo.app.modular.vip.param.AppVipOrderParam;
import vip.xiaonuo.app.modular.vip.service.AppVipOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * vip订单表service接口实现类
 *
 * @author 郑雄伟
 * @date 2023-10-07 14:48:05
 */
@Service
public class AppVipOrderServiceImpl extends ServiceImpl<AppVipOrderMapper, AppVipOrder> implements AppVipOrderService {

    @Override
    public AppVipOrder tradeWapPay(Long userId, String mobileSpec, Integer packageId, BigDecimal money, Integer payType) {
        AppVipOrder info = new AppVipOrder();
        //订单号
        info.setOrderNo("vip" + IdWorker.getId());
        //用户id
        info.setUserId(userId);
        //设备型号
        info.setMobileSpec(mobileSpec);
        //产品id
        info.setVipPackageId(packageId);
        //消费金额
        info.setMoney(money);
        //支付类型
        info.setPayType(payType);
        this.save(info);
        return info;
    }

    @Override
    public boolean successCallBack(String out_trade_no, String trade_no) {
        return false;
    }

    @Override
    public PageResult<AppVipOrder> page(AppVipOrderParam appVipOrderParam) {
        QueryWrapper<AppVipOrder> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(appVipOrderParam)) {

            // 根据用户id 查询
            if (ObjectUtil.isNotEmpty(appVipOrderParam.getUserId())) {
                queryWrapper.lambda().eq(AppVipOrder::getUserId, appVipOrderParam.getUserId());
            }
            // 根据订单号 查询
            if (ObjectUtil.isNotEmpty(appVipOrderParam.getOrderNo())) {
                queryWrapper.lambda().eq(AppVipOrder::getOrderNo, appVipOrderParam.getOrderNo());
            }
            // 根据app_vip_package表主键id 查询
            if (ObjectUtil.isNotEmpty(appVipOrderParam.getVipPackageId())) {
                queryWrapper.lambda().eq(AppVipOrder::getVipPackageId, appVipOrderParam.getVipPackageId());
            }
            // 根据支付类型 1支付宝  2微信  3苹果  4杉德支付宝 5杉德微信 查询
            if (ObjectUtil.isNotEmpty(appVipOrderParam.getPayType())) {
                queryWrapper.lambda().eq(AppVipOrder::getPayType, appVipOrderParam.getPayType());
            }
            // 根据0待支付 1已支付 查询
            if (ObjectUtil.isNotEmpty(appVipOrderParam.getStatus())) {
                queryWrapper.lambda().eq(AppVipOrder::getStatus, appVipOrderParam.getStatus());
            }
            // 根据设备型号（为空取设备平台编码） 查询
            if (ObjectUtil.isNotEmpty(appVipOrderParam.getMobileSpec())) {
                queryWrapper.lambda().eq(AppVipOrder::getMobileSpec, appVipOrderParam.getMobileSpec());
            }
            // 根据金额 查询
            if (ObjectUtil.isNotEmpty(appVipOrderParam.getPrice())) {
                queryWrapper.lambda().eq(AppVipOrder::getMoney, appVipOrderParam.getPrice());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<AppVipOrder> list(AppVipOrderParam appVipOrderParam) {
        return this.list();
    }

    @Override
    public void add(AppVipOrderParam appVipOrderParam) {
        AppVipOrder appVipOrder = new AppVipOrder();
        BeanUtil.copyProperties(appVipOrderParam, appVipOrder);
        this.save(appVipOrder);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<AppVipOrderParam> appVipOrderParamList) {
        appVipOrderParamList.forEach(appVipOrderParam -> {
            this.removeById(appVipOrderParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(AppVipOrderParam appVipOrderParam) {
        AppVipOrder appVipOrder = this.queryAppVipOrder(appVipOrderParam);
        BeanUtil.copyProperties(appVipOrderParam, appVipOrder);
        this.updateById(appVipOrder);
    }

    @Override
    public AppVipOrder detail(AppVipOrderParam appVipOrderParam) {
        return this.queryAppVipOrder(appVipOrderParam);
    }

    /**
     * 获取vip订单表
     *
     * @author 郑雄伟
     * @date 2023-10-07 14:48:05
     */
    private AppVipOrder queryAppVipOrder(AppVipOrderParam appVipOrderParam) {
        AppVipOrder appVipOrder = this.getById(appVipOrderParam.getId());
        if (ObjectUtil.isNull(appVipOrder)) {
            throw new ServiceException(AppVipOrderExceptionEnum.NOT_EXIST);
        }
        return appVipOrder;
    }

    @Override
    public void export(AppVipOrderParam appVipOrderParam) {
        List<AppVipOrder> list = this.list(appVipOrderParam);
        PoiUtil.exportExcelWithStream("SnowyAppVipOrder.xls", AppVipOrder.class, list);
    }

}
